package org.example.security.component;

import org.example.common.constant.Issuer;
import org.example.security.util.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class JwtFilter extends OncePerRequestFilter {

    @Autowired
    @Qualifier("SecurityUserDetailsService")
    private UserDetailsService userDetailsService;

    @Autowired
    @Qualifier("SecurityWxUserDetailsService")
    private UserDetailsService wxUserDetailService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;


    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                    FilterChain filterChain) throws ServletException, IOException {

        String authToken = httpServletRequest.getHeader(this.tokenHeader);
        if (authToken == null) {
            filterChain.doFilter(httpServletRequest, httpServletResponse);
            return;
        }

        String issuer = jwtTokenUtil.getIssuerFromToken(authToken);
        String subject = jwtTokenUtil.getSubjectFromToken(authToken);
        if (issuer == null || subject == null){
            filterChain.doFilter(httpServletRequest, httpServletResponse);
            return;
        }

        if (SecurityContextHolder.getContext().getAuthentication() == null){
            UserDetails userDetails = null;
            if (issuer.equals(Issuer.fromAdmin)){
                userDetails = userDetailsService.loadUserByUsername(subject);
            }else if(issuer.equals(Issuer.fromWx)){
                userDetails = wxUserDetailService.loadUserByUsername(subject);
            }else {
                filterChain.doFilter(httpServletRequest, httpServletResponse);
                return;
            }
            if (jwtTokenUtil.validateToken(authToken, userDetails)) {

                UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
                        userDetails, null, userDetails.getAuthorities());
                authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));

                SecurityContextHolder.getContext().setAuthentication(authentication);
            }
        }

        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }
}
